home *** CD-ROM | disk | FTP | other *** search
/ SGI Performance Co-Pilot 1.3 / SGI Performance Co-Pilot 1.3.iso / dist / pcp.idb / usr / pcp / bin / cron.pmcheck.z / cron.pmcheck
Text File  |  1997-04-03  |  7KB  |  322 lines

  1. #!/bin/sh
  2. #
  3. # Copyright 1995, Silicon Graphics, Inc.
  4. # ALL RIGHTS RESERVED
  5. # UNPUBLISHED -- Rights reserved under the copyright laws of the United
  6. # States.   Use of a copyright notice is precautionary only and does not
  7. # imply publication or disclosure.
  8. # U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
  9. # Use, duplication or disclosure by the Government is subject to restrictions
  10. # as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
  11. # in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
  12. # in similar or successor clauses in the FAR, or the DOD or NASA FAR
  13. # Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
  14. # 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
  15. # THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
  16. # INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
  17. # DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
  18. # PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
  19. # GRAPHICS, INC.
  20. #
  21. # Example administrative script to check pmlogger instances are alive,
  22. # and restart as required.
  23. #
  24.  
  25. # constant setup
  26. #
  27. tmp=/tmp/$$
  28. status=0
  29. trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15
  30. prog=`basename $0`
  31.  
  32. # control file for pmlogger administration ... edit the entries in this
  33. # file to reflect your local configuration
  34. #
  35. CONTROL=/var/pcp/config/pmlogger/control
  36.  
  37. # determine real name for localhost
  38. LOCALHOSTNAME="localhost"
  39. [ -x /usr/bsd/hostname ] && LOCALHOSTNAME=`/usr/bsd/hostname`
  40. LOGDIR=${PCP_LOGDIR-/var/adm/pcplog}/$LOCALHOSTNAME
  41.  
  42. # default location
  43. #
  44. logfile=pmlogger.log
  45.  
  46. # option parsing
  47. #
  48. ECHO=false
  49. usage="Usage: $prog [-c control] [-n]"
  50. while getopts c:n? c
  51. do
  52.     case $c
  53.     in
  54.     c)    CONTROL="$OPTARG"
  55.         ;;
  56.     n)    ECHO=true
  57.         ;;
  58.     ?)    echo "$usage"
  59.         status=1
  60.         exit
  61.         ;;
  62.     esac
  63. done
  64. shift `expr $OPTIND - 1`
  65.  
  66. if [ $# -ne 0 ]
  67. then
  68.     echo "$usage"
  69.     status=1
  70.     exit
  71. fi
  72.  
  73. if [ ! -f $CONTROL ]
  74. then
  75.     echo "$prog: Error: cannot find control file ($CONTROL)"
  76.     status=1
  77.     exit
  78. fi
  79.  
  80. _error()
  81. {
  82.     echo "$prog: [$CONTROL:$line]"
  83.     echo "Error: $1"
  84.     echo "... logging for host \"$host\" unchanged"
  85.     touch $tmp.err
  86. }
  87.  
  88. _warning()
  89. {
  90.     echo "$prog [$CONTROL:$line]"
  91.     echo "Warning: $1"
  92. }
  93.  
  94. _get_logfile()
  95. {
  96.     set - $args
  97.  
  98.     # ignore errors, as only looking for -l
  99.     #
  100.     while getopts l: c >/dev/null 2>&1
  101.     do
  102.     case $c
  103.     in
  104.         l)    logfile="$OPTARG"
  105.         ;;
  106.         esac
  107.     done
  108. }
  109.  
  110. _check_logfile()
  111. {
  112.     if [ ! -f $logfile ]
  113.     then
  114.     echo "Cannot find pmlogger output file at \"$logfile\""
  115.     else
  116.     echo "Contents of pmlogger output file \"$logfile\" ..."
  117.     cat $logfile
  118.     fi
  119. }
  120.  
  121. _check_logger()
  122. {
  123.     # wait until pmlogger process starts, or exits
  124.     #
  125.     delay=5
  126.     [ ! -z "$PMCD_CONNECT_TIMEOUT" ] && delay=$PMCD_CONNECT_TIMEOUT
  127.     x=5
  128.     [ ! -z "$PMCD_REQUEST_TIMEOUT" ] && x=$PMCD_REQUEST_TIMEOUT
  129.  
  130.     # wait for maximum time of a connection and 20 requests
  131.     #
  132.     delay=`expr $delay + 20 \* $x`
  133.     i=0
  134.     while [ $i -lt $delay ]
  135.     do
  136.     echo ".\c"
  137.     if echo "connect $1" | pmlc >$tmp.out 2>&1
  138.     then
  139.         if grep "Unable to connect" $tmp.out >/dev/null
  140.         then
  141.         :
  142.         else
  143.         sleep 5
  144.         echo " done"
  145.         return 0
  146.         fi
  147.     fi
  148.     if ps -ef | grep pmlogger | nawk '
  149. BEGIN        { sts=0 }
  150. $2 == '$1'    { sts=1; exit sts }
  151. END        { exit sts }'
  152.     then
  153.         echo " process exited!"
  154.         _check_logfile
  155.         return 1
  156.     fi
  157.     sleep 5
  158.     i=`expr $i + 5`
  159.     done
  160.     echo " timed out waiting!"
  161.     sed -e 's/^/    /' $tmp.out
  162.     _check_logfile
  163.     return 1
  164. }
  165.  
  166. # note on control file format version
  167. #  1.0 was shipped as part of PCPWEB beta, and did not include the
  168. #    socks field [this is the default for backwards compatibility]
  169. #  1.0 is the first production release, and the version is set in
  170. #    the control file with a $version=1.1 line (see below)
  171. #
  172.  
  173. if /sbin/chkconfig pmlogger
  174. then
  175.     echo $LOGDIR >$tmp.dir
  176. else
  177.     echo >$tmp.dir
  178. fi
  179.  
  180. rm -f $tmp.err
  181. line=0
  182. version=1.0
  183. cat $CONTROL \
  184. | sed -e "s/LOCALHOSTNAME/$LOCALHOSTNAME/g" \
  185. | while read host primary socks dir args
  186. do
  187.     line=`expr $line + 1`
  188.     case "$host"
  189.     in
  190.     \#*|'')    # comment or empty
  191.         continue
  192.         ;;
  193.     \$*)    # in-line shell command
  194.         cmd=`echo "$host $primary $socks $dir $args" | sed -e 's/^\\$//'`
  195.         eval $cmd
  196.         continue
  197.         ;;
  198.     esac
  199.  
  200.     if [ "$version" = "1.0" ]
  201.     then
  202.     args="$dir $args"
  203.     dir="$socks"
  204.     socks=n
  205.     fi
  206.  
  207.     # make sure output directory exists
  208.     #
  209.     if [ ! -d $dir ]
  210.     then
  211.     mkdir -p $dir
  212.     if [ ! -d $dir ]
  213.     then
  214.         _error "cannot create directory ($dir) for PCP archive files"
  215.     else
  216.         _warning "creating directory ($dir) for PCP archive files"
  217.     fi
  218.     fi
  219.  
  220.     [ ! -d $dir ] && continue
  221.  
  222.     # check for directory duplicate entries
  223.     #
  224.     if grep $dir $tmp.dir >/dev/null
  225.     then
  226.     if [ "X$primary" != Xy ]
  227.     then
  228.         _error "Cannot start more than one pmlogger instance for archive directory \"$dir\""
  229.         continue
  230.     fi
  231.     else
  232.     echo "$dir" >>$tmp.dir
  233.     fi
  234.  
  235.     cd $dir
  236.     $ECHO && echo "+ cd $dir"
  237.  
  238.     if [ "X$primary" = Xy ]
  239.     then
  240.     if [ "X$host" != "X$LOCALHOSTNAME" ]
  241.     then
  242.         _error "\"primary\" only allowed for $LOCALHOSTNAME (localhost, not $host)"
  243.         continue
  244.     fi
  245.     if /sbin/chkconfig pmlogger
  246.     then
  247.         :
  248.     else
  249.         _error "primary logging disabled via chkconfig for $host"
  250.         continue
  251.     fi
  252.  
  253.     pid=`ps -ef \
  254.          | sed -n \
  255.          -e '/grep/d' \
  256.          -e 's/$/ /' \
  257.          -e '/\/usr\/pcp\/bin\/pmlogger .*-P /p' \
  258.          | nawk '{print $2}'`
  259.     else
  260.     pid=`ps -ef \
  261.          | sed -n \
  262.          -e '/grep/d' \
  263.          -e 's/$/ /' \
  264.          -e "/\/usr\/pcp\/bin\/pmlogger .*-h *$host /p" \
  265.          | nawk '{print $2}'`
  266.     fi
  267.  
  268.     if [ "X$pid" = X ]
  269.     then
  270.     rm -f Latest
  271.  
  272.     if [ "X$primary" = Xy ]
  273.     then
  274.         args="-P $args"
  275.         iam=" primary"
  276.         # clean up port-map, just in case
  277.         #
  278.         PM_LOG_PORT_DIR=/var/tmp/pmlogger
  279.         rm -f $PM_LOG_PORT_DIR/primary $PM_LOG_PORT_DIR/vcr
  280.     else
  281.         args="-h $host $args"
  282.         iam=""
  283.     fi
  284.  
  285.     # each new log started is named yymmdd.hh.mm
  286.     #
  287.     LOGNAME=`date "+%y%m%d.%H.%M"`
  288.  
  289.     echo "Restarting$iam pmlogger for host \"$host\" ...\c"
  290.     sock_me=''
  291.     [ "$socks" = y ] && sock_me='pmsocks '
  292.  
  293.     _get_logfile
  294.     [ -f $logfile ] && mv -f $logfile $logfile.prior
  295.  
  296.     if $ECHO
  297.     then
  298.         echo
  299.         echo "+ ${sock_me}/usr/pcp/bin/pmlogger $args $LOGNAME"
  300.         continue
  301.     else
  302.         ${sock_me}/usr/pcp/bin/pmlogger $args $LOGNAME >$tmp.out 2>&1 &
  303.         pid=$!
  304.     fi
  305.  
  306.     # wait for pmlogger to get started, and check on its health
  307.     _check_logger $pid
  308.  
  309.     # the archive folio Latest is for the most recent archive in
  310.     # this directory
  311.     #
  312.     [ -f $LOGNAME.0 ] && /usr/pcp/bin/mkaf $LOGNAME.0 >Latest
  313.     fi
  314.  
  315. done
  316.  
  317. [ -f $tmp.err ] && status=1
  318. exit
  319.